// pages/add_share/add_share.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    modalName:'',
    host:app.globalData.host,
    topicList:[],
    selectTopicList:[],
  },
  addTopic:function(e){
    if(this.data.selectTopicList.length >= 3){
      util.error('最多3个话题');return ;
    }
    var index = e.target.dataset.index,selected = this.data.topicList[index],list = this.data.topicList,
    selected_list =  this.data.selectTopicList;
    selected_list.push(selected);
    list.splice(index,1);
    this.setData({
      topicList:list,
      selectTopicList:selected_list,
    })
  },
  deleteTopic:function(e){
    var index = e.target.dataset.index,selected = this.data.selectTopicList[index],list = this.data.topicList,
    selected_list =  this.data.selectTopicList;
    list.push(selected);
    selected_list.splice(index,1);
    this.setData({
      topicList:list,
      selectTopicList:selected_list,
    })
  },
  showModal:function(){
    this.setData({
      modalName: 'modal1',
    })
  },
  hideModal:function(){
    this.setData({
      modalName: '',
    })
  },
  ChooseImage() {
    var _this = this,imgList = _this.data.imgList;
    util.uploadImage(function (res) {
      if(res.code == 1){
        imgList.push(res.data[0]['path']);
        _this.setData({
          imgList: imgList,
        });
      }else{
        util.fail(res.msg,_this)
      }
    },6-_this.data.imgList.length)
  },
  ViewImage(e) {
    wx.previewImage({
      urls: [e.currentTarget.dataset.url],
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    this.data.imgList.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      imgList: this.data.imgList
    })       
  },
  submit: function (e) {
    var data = e.detail.value,topicList = [];
    for (let index = 0; index < this.data.selectTopicList.length; index++) {
      const element = this.data.selectTopicList[index];
      topicList.push(element.id);
    }
    var that = this
    data.image = that.data.imgList;
    data.child_id = app.globalData.parent.baby_default_id;
    data.topicList = topicList;
    if (data.title == ''){
      util.fail("请填写标题", that)
      return
    }
    if (data.content == ''){
      util.fail("请填写分享内容", that)
      return
    }

  util.request('share/add',data,
      function (data) {
        if(data.code == 1){
          util.success(data.msg);
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1500)

        }else{
          util.fail(data.msg,that);
        }
      });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var topic_id = e.topic_id;
    var that = this;
    util.request('topic/index',{},function (data) {
      if(data.code == 1){
        that.setData({
          topicList:data.data.share,
        })
        // if(topic_id){
        //     for (let index = 0; index < that.data.topicList.length; index++) {
        //       const element =  that.data.topicList[index];
        //       if(element.id ==  topic_id){
        //         var list = that.data.topicList,selected_list =  that.data.selectTopicList;
        //         selected_list.push(element);
        //         list.splice(index,1);
        //         that.setData({
        //           topicList:list,
        //           selectTopicList:selected_list,
        //         })
        //       }
        //     }
        // }
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})